<?php
/**
  * 公共控制器
  * date 2013-1-31
  * 封装公共方法
  */ 
class CommonAction extends Action{

  public function _initialize()
  {
     $link=M('Link');
     $tag=M('Tag');
     $this->assign('bottomLinks',$link->field('keywords,url')->where("status=1")->order('id DESC')->select());
     $this->assign('bottomTags',$tag->field('title,code')->order('hit DESC')->limit(14)->select());
  }

  public function _empty(){
      header("HTTP/1.1 404 Not Found"); 
      $this->display('Public:404');
  }

  protected function getAboutTag($articleId)
  {
    $tagArticle=M('TagArticle');
    $tag=M('Tag');
    $where['value_id']=$articleId;
    $allTagIdArray=$tagArticle->field('tag_id')->where($where)->select();
    if(empty($allTagIdArray))
    {
      return 0;
    }else
        {
          $allTagIdStr='';
          foreach($allTagIdArray as $d)
          {
             $allTagIdStr.=','.$d['tag_id'];
          }
          $allTagIdStr=substr($allTagIdStr,1);
          $where2['id']=array('in',$allTagIdStr);
          $tags=$tag->where($where2)->select();
          return $tags;
        }
  }

  protected function upHit($model,$id)
  {
    $obj=M($model);
    $obj->where('id='.$id)->setInc('hit',1);
  }

  protected function getPrevInfo($model,$id)
  {
    $obj=M($model);
    $where['status']=1;
    $where['id']=array('lt',$id);
    return $obj->field('id,title')->where($where)->order('id DESC')->find();
  }

  protected function getNextInfo($model,$id)
  {
    $obj=M($model);
    $where['status']=1;
    $where['id']=array('gt',$id);
    return $obj->field('id,title')->where($where)->order('id ASC')->find();
  } 

  protected function childComment($cmt)
  {
    $comment=M('Comment');
    foreach($cmt as $k=>$d)
    {
      $where['parent_id']=$d['id'];
      $where['status']=1;
      $child=$comment->field('username,qq,email,content,add_time')->where($where)->find();
      if(!empty($child))
      {
        $cmt[$k]['child']=$child;
      }else
          {
            $cmt[$k]['child']=0;
          }
    }
    return $cmt;
  } 

}
?>